package com.shuai.orderfood.merchants.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuai.orderfood.common.Constant;
import com.shuai.orderfood.common.HttpStatusCodeEnum;
import com.shuai.orderfood.exceptions.OrderFoodException;
import com.shuai.orderfood.merchants.mapper.MerchantsUserMapper;
import com.shuai.orderfood.merchants.pojo.MerchantsUser;
import com.shuai.orderfood.merchants.service.MerchantsUserService;
import com.shuai.orderfood.merchants.vo.MerchantsUserVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MerchantsUserServiceImpl extends ServiceImpl<MerchantsUserMapper, MerchantsUser> implements MerchantsUserService {

    @Resource
    private MerchantsUserMapper merchantsUserMapper;


    @Override
    public List<MerchantsUserVo> getAll(String keyword, Integer page, Integer limit) throws Exception {
        page = page == null ? Constant.DEFAULT_PAGE : page;
        limit = limit == null ? Constant.DEFAULT_LIMIT : limit;
        IPage<MerchantsUser> iPage = new Page<>(page, limit);
        LambdaQueryWrapper<MerchantsUser> lambdaQueryWrapper = null;
        if (StringUtils.hasLength(keyword)) {
            lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.like(MerchantsUser::getUsername, keyword);
        }
        IPage<MerchantsUser> merchantsUserIPage = merchantsUserMapper.selectPage(iPage, lambdaQueryWrapper);
        List<MerchantsUser> merchantsUserList = merchantsUserIPage.getRecords();
        List<MerchantsUserVo> merchantsUserVoList = new ArrayList<>();
        for (MerchantsUser merchantsUser : merchantsUserList) {
            MerchantsUserVo merchantsUserVo = new MerchantsUserVo();
            merchantsUserVo.setUserId(merchantsUser.getUserId());
            merchantsUserVo.setUsername(merchantsUser.getUsername());
            merchantsUserVo.setPhone(merchantsUser.getPhone());
            merchantsUserVo.setRoleId(merchantsUser.getRoleId());
            merchantsUserVo.setIsSuper(merchantsUser.getIsSuper());
            merchantsUserVo.setCreateTime(merchantsUser.getCreateTime());
            merchantsUserVoList.add(merchantsUserVo);
        }
        return merchantsUserVoList;
    }

    @Override
    public void updateMerchantsUser(MerchantsUserVo merchantsUserVo) throws Exception {
        System.out.println("merchantsUserVo:" + merchantsUserVo.getUserId());
        System.out.println("merchantsUserVo:" + merchantsUserVo.getUsername());
        System.out.println("merchantsUserVo:" + merchantsUserVo.getPhone());
        //先根据id查询出该用户
        MerchantsUser merchantsUser = merchantsUserMapper.selectById(merchantsUserVo.getUserId());
        if (merchantsUser == null) {
            throw new OrderFoodException(HttpStatusCodeEnum.USERNOTEXIST.getCode(), HttpStatusCodeEnum.USERNOTEXIST.getMessage());
        }

        //判断用户名和电话是否为空
        String username = merchantsUserVo.getUsername();
        String phone = merchantsUserVo.getPhone();
        if(!StringUtils.hasLength(username)) {
            throw new OrderFoodException(HttpStatusCodeEnum.USERNAMENOTNULL.getCode(), HttpStatusCodeEnum.USERNAMENOTNULL.getMessage());
        }
        if(!StringUtils.hasLength(phone)) {
            throw new OrderFoodException(HttpStatusCodeEnum.PHONENOTNULL.getCode(), HttpStatusCodeEnum.PHONENOTNULL.getMessage());
        }

        //判断修改后的用户名是否已存在
        LambdaQueryWrapper<MerchantsUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MerchantsUser::getUsername,username.trim());
        Long count = merchantsUserMapper.selectCount(lambdaQueryWrapper);
        if(count > 0) {
            throw new OrderFoodException(HttpStatusCodeEnum.USERNAMEEXIST.getCode(), HttpStatusCodeEnum.USERNAMEEXIST.getMessage());
        }

        //判断修改后的电话号码是否已存在
        lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MerchantsUser::getPhone,phone.trim());
        count = merchantsUserMapper.selectCount(lambdaQueryWrapper);
        if(count > 0) {
            throw new OrderFoodException(HttpStatusCodeEnum.PHONEEXIST.getCode(), HttpStatusCodeEnum.PHONEEXIST.getMessage());
        }

        //修改该用户信息
        merchantsUser.setUsername(username.trim());
        merchantsUser.setPhone(phone.trim());
        merchantsUser.setUpdateTime(LocalDateTime.now());
        merchantsUserMapper.updateById(merchantsUser);
    }
}
